package com.dstz.bpm.engine.util;

import com.dstz.base.core.util.AppUtil;
import com.dstz.base.core.util.StringUtil;
import com.dstz.bpm.api.engine.action.cmd.ActionCmd;
import com.dstz.sys.api.groovy.IGroovyScriptEngine;
import java.lang.reflect.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HandlerUtil {
   protected static Logger LOG = LoggerFactory.getLogger(HandlerUtil.class);

   public static void a(ActionCmd actionModel, String handler) throws Exception {
      if (!StringUtil.isEmpty(handler)) {
         if (handler.indexOf("(") != -1) {
            g(handler);
         } else {
            String[] aryHandler = handler.split("[.]");
            if (aryHandler != null && aryHandler.length == 2) {
               String beanId = aryHandler[0];
               String method = aryHandler[1];
               Object serviceBean = AppUtil.getBean(beanId);
               if (serviceBean != null) {
                  Method invokeMethod = serviceBean.getClass().getDeclaredMethod(method, ActionCmd.class);
                  invokeMethod.invoke(serviceBean, actionModel);
               }
            }
         }
      }
   }

   private static void g(String script) {
      IGroovyScriptEngine groovyEngine = (IGroovyScriptEngine)AppUtil.getBean(IGroovyScriptEngine.class);
      groovyEngine.execute(script);
   }
}
